#! /usr/bin/env tcsh

set ddir = nifti_regress_data
set prog = nifti_tool   # might allow this to change

set grind = 0           # do we run with valgrind?

if ( $#argv > 0 ) then
    if ( "$argv[1]" == "-grind" ) then
        set grind = 1
    else
        echo "usage: `basename $0` [-grind]"
        exit
    endif
endif

# make sure $prog at least exists
# $prog -ver >& /dev/null
# if ( $status ) then
#     echo "missing program '$prog' (needs to be in PATH)"
#     exit
# endif

# set date and output dir (based on grind)
set date = `date +%Y_%m_%d_%H%M`
if ( $grind ) then
    set odir = vg_results_${prog}_${date}
else
    set odir = results_${prog}_${date}
endif

# note command files (in commands dir)
if ( ! -d commands ) then
    echo "** failure: missing commands directory"
    exit
endif

cd commands ; set cfiles = ( c* ) ; cd ..

if ( $#cfiles < 1 ) then
    echo "** failure: no command files found in 'commands'"
    exit
endif

# create the output directory
if ( -e $odir ) then
    echo "** failure: output directory '$odir' already exists"
    exit
endif

mkdir $odir

if ( $status ) then
    echo "** failure: output directory '$odir' already exists"
    exit
endif

# copy data files into output dir
cp $ddir/* $odir

cd $odir

# now begin the work

echo "++ using `which $prog` ..."

if ( $grind ) then
    alias $prog "valgrind --leak-resolution=high --leak-check=full $prog"
endif

foreach cfile ( $cfiles )
    echo -n $cfile ...
    echo "--------------------------------------------------------" > e.$cfile
    cat ../commands/$cfile >> e.$cfile
    echo "" >> e.$cfile
    echo "--------------------------" >> e.$cfile
    echo "" >> e.$cfile
    source ../commands/$cfile >>& e.$cfile   # to keep alias
    echo done
end
